package com.fan.leetcode;

import org.junit.jupiter.api.Test;

import java.util.*;

/**
 * 1418. 点菜展示表
 *
 * @author ZhangFan
 * @since 2021/8/3 下午 10:25
 */
public class Question1418 {
    @Test
    public void test1418() {
        List<List<String>> orders = new ArrayList<>();
        orders.add(Arrays.asList("David", "3", "Ceviche"));
        orders.add(Arrays.asList("Corina", "10", "Beef Burrito"));
        orders.add(Arrays.asList("David", "3", "Fried Chicken"));
        orders.add(Arrays.asList("Carla", "5", "Water"));
        orders.add(Arrays.asList("Carla", "5", "Ceviche"));
        orders.add(Arrays.asList("Rous", "3", "Ceviche"));
        System.out.println(displayTable(orders));
    }

    public List<List<String>> displayTable(List<List<String>> orders) {
        Set<Integer> tables = new HashSet<>();
        Set<String> titles = new HashSet<>();
        for (List<String> order : orders) {
            tables.add(Integer.valueOf(order.get(1)));
            titles.add(order.get(2));
        }
        List<Integer> tableList = new ArrayList<>(tables);
        LinkedList<String> titleList = new LinkedList<>(titles);
        Collections.sort(tableList);
        Collections.sort(titleList);
        titleList.addFirst("Table");
        List<List<String>> lists = new ArrayList<>();
        lists.add(titleList);

        List<String> strings = new ArrayList<>();
        for (String s : titleList) {
            strings.add("0");
        }
        for (Integer s : tableList) {
            List<String> list = new ArrayList<>(strings);
            list.set(0, String.valueOf(s));
            lists.add(list);
        }

        for (List<String> order : orders) {
            int tableIndex = tableList.indexOf(Integer.parseInt(order.get(1)));
            int titleIndex = titleList.indexOf(order.get(2));
            List<String> table = lists.get(tableIndex + 1);
            table.set(titleIndex, String.valueOf(Integer.parseInt(table.get(titleIndex)) + 1));
        }
        return lists;
    }
}
